package MaintMenu.PoAnalysis;// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   MRAnalysis.java

import java.sql.*;

// Referenced classes of package pk1:
//            DnkFunctions

public class MRAnalysis {

    public MRAnalysis(String from, String to) {
        try {
            DnkFunctionsmr dnk = new DnkFunctionsmr();
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String dataSourceName = "dbLI";
            String dbURL = (new StringBuilder()).append("jdbc:odbc:").append(dataSourceName).toString();
            Connection con = DriverManager.getConnection(dbURL, "", "");
            Statement st = con.createStatement();
            Statement st1 = con.createStatement();
            Statement st2 = con.createStatement();
            Statement st3 = con.createStatement();
            try {
                st.execute("DELETE * FROM TBLDRSALE;");
                st.execute("DELETE * FROM TBLDRSALE1;");
                st.execute("DELETE * FROM TBLDRSALE2;");
                st.execute((new StringBuilder()).append("INSERT INTO TBLDRSALE1([PART#],DRSALE) SELECT C.[PART#], SUM(C.QUANTITY) AS DRSale FROM TBLARINVOICES AS B INNER JOIN TBLARINVOICEDETAIL AS C ON B.ARINVOICEID=C.ARINVOICEID WHERE (B.[DATE] BETWEEN #").append(from).append("# AND #").append(to).append("#) ").append("AND (NOT EXISTS (SELECT * FROM TBLCUSTOMERS A ").append("WHERE B.CUSTOMERID=A.CUSTOMERID AND ").append("A.[CUST#] IN('DNK333','DNK777'))) ").append("AND (NOT ISNULL(C.[PART#])) GROUP BY C.[PART#];").toString());
                st.execute((new StringBuilder()).append("INSERT INTO TBLDRSALE2([PART#],DRSALE) SELECT C.[PART#], SUM(C.QUANTITY) AS DRSale FROM TBLARINVOICESB AS B INNER JOIN TBLARINVOICEDETAILB AS C ON B.ARINVOICEID=C.ARINVOICEID WHERE (B.[DATE] BETWEEN #").append(from).append("# AND #").append(to).append("#) ").append("AND (NOT EXISTS (SELECT * FROM TBLCUSTOMERSB A ").append("WHERE B.CUSTOMERID=A.CUSTOMERID AND ").append("A.[CUST#] IN('DNK333','DNK777'))) ").append("AND (NOT ISNULL(C.[PART#])) GROUP BY C.[PART#];").toString());
                st.execute("INSERT INTO TBLDRSALE([PART#],DRSALE) SELECT [PART#],0 FROM TBLINVENTORY;");
                st.execute("UPDATE TBLDRSALE A INNER JOIN TBLDRSALE1 B ON A.[PART#] = B.[PART#] SET A.DRSALE=A.DRSALE+B.DRSALE;");
                st.execute("UPDATE TBLDRSALE A INNER JOIN TBLDRSALE2 B ON A.[PART#] = B.[PART#] SET A.DRSALE=A.DRSALE+B.DRSALE;");
//                st.execute("INSERT INTO TBLMRN ( [PART#], SUPPLIER, DESCRIPTION, T24m, T12m, 12MSL, 9msl, 6MSL, 3MSL, INSTOCK, 1mns, 1M, 1_5M, 2M, 3M, 3_5M, 4M, 5M, ONORDER, SHIPPING, TYG_TW, TYG_TX, GD, GN, KS, TYG_TW_P, TYG_TX_P, GD_P, GN_P, KS_P, TYG_TW_K, TYG_TX_K, GD_K, GN_K, KS_K, [TYG_TW#], [TYG_TX#], [GD#], [GN#], [KS#], REMARK, DNK_MEMO, TYG_TW_MEMO, TYG_TX_MEMO, GD_MEMO, GN_MEMO, KS_MEMO, RATE, TYG_TW_C, TYG_TX_C, GD_C, GN_C, KS_C, TYG_TW_BO, TYG_TX_BO, GD_BO, GN_BO, KS_BO, OEM_NO, PARTLINK, SUGGEST, TYG_TW_STATUS, TYG_TX_STATUS, GD_STATUS, GN_STATUS, KS_STATUS, NEW, CAPA, MANUAL ) SELECT ITEM_NO, NULL, DESCRIPT, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, IIF(ISNULL(TYG_TW_PRC),0,TYG_TW_PRC), IIF(ISNULL(TYG_TX_PRC),0,TYG_TX_PRC), IIF(ISNULL(GD_PRC),0,GD_PRC), IIF(ISNULL(GN_PRC),0,GN_PRC), IIF(ISNULL(AM_PRC),0,AM_PRC), IIF(ISNULL(TYG_TW_PKG),0,TYG_TW_PKG), IIF(ISNULL(TYG_TX_PKG),0,TYG_TX_PKG), IIF(ISNULL(GD_PKG),0,GD_PKG), IIF(ISNULL(GN_PKG),0,GN_PKG), IIF(ISNULL(AM_PKG),0,AM_PKG), TYG_TW_NO, TYG_TX_NO, GD_NO, GN_NO, AM_NO, NULL, REMARK, TYG_REMARK, TX_REMARK, GD_REMARK, GN_REMARK, NULL, 0, IIF(ISNULL(TYG_CUFT),0,TYG_CUFT), IIF(ISNULL(TYG_CUFT),0,TYG_CUFT), IIF(ISNULL(GD_CUFT),0,GD_CUFT), 0, 0, 0, 0, 0, 0, 0, PART_NO, PARTLINK, 0, NULL, NULL, NULL, NULL, NULL, 0, 0, 0 FROM MIRRORM1 WHERE ITEM_NO NOT IN (SELECT [PART#] FROM TBLMRN);");
                st.execute("UPDATE TBLMRN SET T24M = 0, T12M = 0, 12MSL = 0, 9MSL = 0, 6MSL = 0, 3MSL = 0, INSTOCK = 0, 1MNS = 0, 1M = 0, 1_5M = 0, 2M = 0, 3M = 0, 3_5M = 0, 4M = 0, 5M = 0, 6M = 0, ONORDER = 0, SHIPPING = 0, TYG_TW = 0, TYG_TX = 0, GD = 0, GN = 0, KS=0, KV=0, DEPO=0, REMARK = NULL, NEW = 0, SUGGEST = 0, MANUAL = 0, TYG_TW_C = IIF([TYG_TW_C] >= 0.001 OR [TYG_TW_C] <= -0.001,Round([TYG_TW_C], 2),0), start = 0, high3 = 0, avg = 0, start5 = 0, high5 = 0, avg5 = 0, start4 = 0, high4 = 0, avg4 = 0;");
                st.execute("UPDATE TBLMRN AS A INNER JOIN TBLINVENTORY AS B ON A.[PART#]=B.[PART#] SET A.DESCRIPTION = B.DESCRIPTION WHERE A.DESCRIPTION <> B.DESCRIPTION OR ISNULL(A.DESCRIPTION);");
                st.execute("UPDATE TBLMRN AS A INNER JOIN TBLINVENTORY AS B ON A.[PART#]=B.[PART#] SET A.POSITION = B.POSITION WHERE A.POSITION <> B.POSITION OR ISNULL(A.POSITION);");
                st.execute("UPDATE TBLMRN SET TYG_TW_K = 0 WHERE ISNULL(TYG_TW_K);");
                st.execute("UPDATE TBLMRN SET GD_K = 0 WHERE ISNULL(GD_K);");
                st.execute("UPDATE TBLMRN SET TYG_TX_K = 0 WHERE ISNULL(TYG_TX_K);");
                st.execute("UPDATE (TBLMRN AS A LEFT JOIN TBLSUMSALE AS B ON A.[PART#]=B.[PART#]) LEFT JOIN TBLSUMSALE1 AS C ON A.[PART#]=C.[PART#] SET A.12MSL = IIF(ISNULL(B.[12MSL]),0,B.[12MSL]) + IIF(ISNULL(C.[12MSL]),0,C.[12MSL]) - (IIF(ISNULL(B.[9MSL]),0,B.[9MSL]) + IIF(ISNULL(C.[9MSL]),0,C.[9MSL])), A.3MSL = IIF(ISNULL(B.[3MSL]),0,B.[3MSL]) + IIF(ISNULL(C.[3MSL]),0,C.[3MSL]), A.6MSL = IIF(ISNULL(B.[6MSL]),0,B.[6MSL]) + IIF(ISNULL(C.[6MSL]),0,C.[6MSL]) - (IIF(ISNULL(B.[3MSL]),0,B.[3MSL]) + IIF(ISNULL(C.[3MSL]),0,C.[3MSL])), A.9MSL = IIF(ISNULL(B.[9MSL]),0,B.[9MSL]) + IIF(ISNULL(C.[9MSL]),0,C.[9MSL]) - (IIF(ISNULL(B.[6MSL]),0,B.[6MSL]) + IIF(ISNULL(C.[6MSL]),0,C.[6MSL])), A.T24M = IIF(ISNULL(B.[24MSL]),0,B.[24MSL]) + IIF(ISNULL(C.[24MSL]),0,C.[24MSL]) - (IIF(ISNULL(B.[12MSL]),0,B.[12MSL]) + IIF(ISNULL(C.[12MSL]),0,C.[12MSL])), A.T12M = IIF(ISNULL(B.[12MSL]),0,B.[12MSL]) + IIF(ISNULL(C.[12MSL]),0,C.[12MSL]);");
                st.execute("UPDATE TBLMRN AS A INNER JOIN TBLINVENTORY AS B ON A.[PART#]=B.[PART#] SET A.INSTOCK = B.INSTOCK+B.INSTOCK1, A.RATE = [3MSL]-[6MSL];");
                st.execute("DELETE * FROM tblonorder;");
                st.execute("INSERT INTO TBLONORDER ( [PART#], ONORDER ) SELECT [PART#], SUM(QTY-SHIPPED) AS ONORDER FROM TBLTEMPPO1 GROUP BY [PART#];");
                st.execute("UPDATE TBLMRN AS a INNER JOIN tblonorder AS b ON a.[part#] = b.[part#] SET a.onorder = B.ONORDER;");
                st.execute("DELETE * FROM TBLSHIPPING;");
                st.execute("INSERT INTO TBLSHIPPING ( [PART#], SHIPPING ) SELECT B.[PART#], SUM(B.amountout) AS SHIPPING FROM TBLPURCHASEORDERS AS A INNER JOIN TBLPURCHASEORDERDETAIL AS B ON A.POID=B.POID WHERE A.CLEARED = 0 AND (NOT ISNULL(B.CONTAINER)) AND PRINTED = -1 GROUP BY B.[PART#];");
                st.execute("UPDATE TBLMRN AS A INNER JOIN TBLSHIPPING AS B ON A.[PART#]=B.[PART#] SET A.SHIPPING = B.SHIPPING;");
                st.execute("SELECT B.[PART#] AS PART, B.CONTAINER AS CON FROM TBLPURCHASEORDERS AS A INNER JOIN TBLPURCHASEORDERDETAIL AS B ON A.POID=B.POID WHERE A.CLEARED = 0 AND (NOT ISNULL(B.CONTAINER)) AND B.AMOUNTOUT <> 0;");
                ResultSet rs = st.getResultSet();
                if (rs != null)
                    while (rs.next()) {
                        String a = rs.getString("CON");
                        String b = rs.getString("PART");
                        try {
                            st2.execute((new StringBuilder()).append("update TBLMRN set REMARK = REMARK & ' ' & '").append(a).append("' WHERE [PART#] = '").append(b).append("';").toString());
                        } catch (SQLException er) {
                        }
                    }
                st.execute("UPDATE TBLMRN SET 1MNS = IIF(IIF(([3MSL]+[6MSL]+[9MSL]+[12MSL])/12>([3MSL]+[6MSL])/6, ([3MSL]+[6MSL]+[9MSL]+[12MSL])/12, ([3MSL]+[6MSL])/6)>[3MSL]/3, IIF(([3MSL]+[6MSL]+[9MSL]+[12MSL])/12>([3MSL]+[6MSL])/6, ([3MSL]+[6MSL]+[9MSL]+[12MSL])/12, ([3MSL]+[6MSL])/6), [3MSL]/3);");
                st.execute("UPDATE TBLMRN SET high3 = [1mns]*3");
                st.execute("UPDATE TBLMRN SET avg = T12m/12");
                st.execute("UPDATE TBLMRN SET high5 = [1mns]*1.5");
                st.execute("UPDATE TBLMRN SET avg5 = high5/5");
                st.execute("UPDATE TBLMRN SET high4 = [1mns]");
                st.execute("UPDATE TBLMRN SET avg4 = high4/4");
                st.execute("UPDATE TBLMRN SET 1M = iif(instock>0,INSTOCK,0) - [1MNS], 1_5M = iif(instock>0,INSTOCK,0) - [1MNS]*1.5, 2M = iif(instock>0,INSTOCK,0) - [1MNS]*2, 3M = iif(instock>0,INSTOCK,0) - [1MNS]*3, 3_5M = iif(instock>0,INSTOCK,0) - [1MNS]*3.5, 4M = iif(instock>0,INSTOCK,0) - [1MNS]*4, 5M = iif(instock>0,INSTOCK,0) - [1MNS]*5, 6M = iif(instock>0,INSTOCK,0) - [1MNS]*6;");
                st.execute("DELETE * FROM TBLMINCOSTINGDATE;");
                st.execute("INSERT INTO TBLMINCOSTINGDATE ( INVID, RDATE ) SELECT INVID, MIN(DATERECEIVED) AS RDATE FROM TBLINVENTORYCOSTING GROUP BY INVID;");
                st.execute("UPDATE TBLMRN AS A INNER JOIN (TBLINVENTORY AS B INNER JOIN TBLMINCOSTINGDATE AS C ON B.INVID=C.INVID) ON A.[PART#]=B.[PART#] SET A.NEW = IIF((C.RDATE >= DATEADD('M',-3,NOW())) AND (B.BEGINYR >= DATEPART('YYYY',DATEADD('YYYY',-4,NOW()))),-1,0);");
                st.execute("DELETE * FROM TBLMRN_TEMP;");
                st.execute("INSERT INTO TBLMRN_TEMP SELECT * FROM TBLMRN;");
                st.execute("UPDATE TBLMRN_temp SET 1MNS = IIF([1MNS] >= 0.1 OR [1MNS] <= -0.1,Round([1MNS], 0), 0), 1M = IIF([1M] >= 0.1 OR [1M] <= -0.1,Round([1M], 0),0), 1_5M = IIF([1_5M] >= 0.1 OR [1_5M] <= -0.1,ROUND([1_5M], 0), 0), 2M = IIF([2M] >= 0.1 OR [2M] <= -0.1,ROUND([2M],0), 0), 3M = IIF([3M] >= 0.1 OR [3M] <= -0.1,ROUND([3M],0), 0), 3_5M = IIF([3_5M] >= 0.1 OR [3_5M] <= -0.1,ROUND([3_5M],0), 0), 4M = IIF([4M] >= 0.1 OR [4M] <= -0.1, ROUND([4M],0), 0), 5M = IIF([5M] >= 0.1 OR [5M] <= -0.1,ROUND([5M],0), 0), 6M = IIF([6M] >= 0.1 OR [6M] <= -0.1,ROUND([6M],0), 0);");
                st.execute("UPDATE TBLMRN_temp SET avg = Round(avg,0), high3= Round(high3,0)");
                st.execute("UPDATE TBLMRN_temp SET start = high3+avg");
                st.execute("UPDATE TBLMRN_temp SET avg5 = Round(avg5,0), high5= Round(high5,0)");
                st.execute("UPDATE TBLMRN_temp SET start5 = high5+avg5");
                st.execute("UPDATE TBLMRN_temp SET avg4 = Round(avg4,0), high4= Round(high4,0)");
                st.execute("UPDATE TBLMRN_temp SET start4 = high4+avg4");
                st.execute("UPDATE TBLMRN_TEMP AS A INNER JOIN TYG_TWBO1 AS B ON A.[PART#] = B.[DNK#] SET A.TYG_TW_BO = B.[B/O];");
                st.execute("UPDATE TBLMRN_TEMP AS A INNER JOIN GDBO1 AS B ON A.[PART#] = B.CUSTNO SET A.GD_BO = B.QTY;");
                st.execute("DELETE * FROM tblmrn_tempout;");
                st.execute((new StringBuilder()).append("INSERT INTO tblmrn_tempout ( [part#], supplier, description, rate, t24m, t12m, 12msl, 9msl, 6msl, 3msl, instock, onorder, shipping, tyg_tw_bo, gd_bo, 1ns, 2m, 3m, 3_5m, 4m, 5m, 6m, start,high3,avg,start4,high4,avg4, gn, tyg_tx, ks, kv, DEPO, tyg_tw, gd, gn_p, tyg_tx_p, ks_p, kv_p, DEPO_p, tyg_tw_p, gd_p, gn_memo, tyg_tx_memo, ks_memo, kv_memo, DEPO_memo, tyg_tw_memo, gd_memo, dnk_memo, [gn#], [tyg_tx#], [ks#], [kv#], [DEPO#], [tyg_tw#], [gd#], remark, oem_no, partlink, gn_k, tyg_tx_k, ks_k, kv_k, DEPO_k, tyg_tw_k, gd_k, new, manual, gn_c, tyg_tx_c, ks_c, kv_c, DEPO_c, tyg_tw_c, gd_c, capa, position, drsale,drdate ) SELECT [part#], supplier, description, rate, [t24m], [t12m], [12msl], [9msl], [6msl], [3msl], instock, onorder, shipping, tyg_tw_bo, gd_bo, [1m], [2m]+(onorder+shipping+tyg_tw_bo+gd_bo), [3m]+(onorder+shipping+tyg_tw_bo+gd_bo), [3_5m]+(onorder+shipping+tyg_tw_bo+gd_bo), [4m]+(onorder+shipping+tyg_tw_bo+gd_bo), [5m]+(onorder+shipping+tyg_tw_bo+gd_bo), [6m]+(onorder+shipping+tyg_tw_bo+gd_bo), start,high3, avg,start4,high4,avg4,gn, tyg_tx, ks, kv, DEPO, tyg_tw, gd, gn_p, tyg_tx_p, ks_p, kv_p, DEPO_p, tyg_tw_p, gd_p, gn_memo, tyg_tx_memo, ks_memo, kv_memo, DEPO_memo, tyg_tw_memo, gd_memo, dnk_memo, [gn#], [tyg_tx#], [ks#], [kv#], [DEPO#], [tyg_tw#], [gd#], remark, oem_no, partlink, gn_k, tyg_tx_k, ks_k, kv_k, DEPO_k, tyg_tw_k, gd_k, new, manual, gn_c, tyg_tx_c, ks_c, kv_c, DEPO_c, tyg_tw_c, gd_c, capa, position, 0, '").append(from).append("' ").append("FROM tblmrn_temp ").append("ORDER BY GN+TYG_TX+KS+KV+TYG_TW+GD DESC;").toString());
                st.execute("UPDATE TBLMRN_TEMPOUT A INNER JOIN TBLDRSALE B ON A.[PART#] = B.[PART#] SET A.DRSALE=B.DRSALE;");
                st.execute("UPDATE tblmrn_tempout AS a inner JOIN tblinventory AS b ON a.[part#] = b.[part#] SET a.beginyr=b.beginyr, a.endyr=b.endyr;");
                st.execute("UPDATE TBLMRN_TEMPOUT AS A INNER JOIN TYG_TWBO1 AS B ON A.[PART#] = B.[DNK#] SET A.TYG_TW_MEMO_T = B.REMARKS;");
                st.execute("UPDATE TBLMRN_TEMPOUT AS A INNER JOIN GDBO1 AS B ON A.[PART#] = B.CUSTNO SET A.GD_MEMO_T = B.REMARK;");
                st.execute("DELETE * FROM tblmrn_tempout2;");
                st.execute("INSERT INTO tblmrn_TEMPOUT2([PART#], Supplier, Description, DRSALE, T24m, T12m, [12msl], [9msl], [6msl], [3msl], ONORDER, SHIPPING, InStock, TYG_TW_BO, GD_BO, 1ns, 2m, 3m, 3_5m, 4m, 5m, 6m, [MOQ], [2W], TYG_TW, GD, TYG_TX, gn, kv, DEPO, ks, TYG_TW_P, GD_P, TYG_TX_P, gn_p, kv_p, DEPO_p, ks_p, TYG_TW_MEMO, TYG_TW_MEMO_T, GD_MEMO, GD_MEMO_T, TYG_TX_MEMO, gn_memo, kv_memo, DEPO_memo, ks_memo, DNK_MEMO, [TYG_TW#], [GD#], [TYG_TX#], [gn#], [kv#], [DEPO#], [ks#], Remark, OEM_NO, PARTLINK, NEW, MANUAL, CAPA, POSITION, DRDATE, TYG_TW_k, GD_k, TYG_TX_k, gn_k, kv_k, DEPO_k, ks_k, TYG_TW_c, GD_c, TYG_TX_c, gn_c, kv_c, DEPO_c, ks_c, beginyr, endyr) SELECT [PART#], Supplier, Description, DRSALE, T24m, T12m, [12msl], [9msl], [6msl], [3msl], ONORDER, SHIPPING, InStock, TYG_TW_BO, GD_BO, [1ns], [2m], [3m], [3_5m], [4m], [5m], [6m], 0, 0, 0, 0, 0, 0, 0, 0, 0, TYG_TW_P, GD_P, TYG_TX_P, gn_p, kv_p, DEPO_p, ks_p, TYG_TW_MEMO, TYG_TW_MEMO_T, GD_MEMO, GD_MEMO_T, TYG_TX_MEMO, gn_memo, kv_memo, DEPO_memo, ks_memo, DNK_MEMO, [TYG_TW#], [GD#], [TYG_TX#], [gn#], [kv#], [DEPO#], [ks#], Remark, OEM_NO, PARTLINK, NEW, MANUAL, CAPA, POSITION, DRDATE, TYG_TW_k, GD_k, TYG_TX_k, gn_k, kv_k, DEPO_k, ks_k, TYG_TW_c, GD_c, TYG_TX_c, gn_c, kv_c, DEPO_c, ks_c, beginyr, endyr FROM tblmrn_TEMPOUT;");
                st.execute("select * from tblmrn_tempout2;");
                ResultSet rs2 = st.getResultSet();
                if (rs2 != null) {
                    double tyg_tw_p = 0.0D;
                    double tyg_tx_p = 0.0D;
                    double gd_p = 0.0D;
                    double gn_p = 0.0D;
                    double kv_p = 0.0D;
                    double depo_p = 0.0D;
                    double ks_p = 0.0D;
                    double tyg_tw_bo = 0.0D;
                    double gd_bo = 0.0D;
                    double instock = 0.0D;
                    double shipping = 0.0D;
                    double onorder = 0.0D;
                    int t12m = 0;
                    int t3msl = 0;
                    double needBuy4 = 0.0D;
                    double needBuy5 = 0.0D;
                    double need3m = 0.0D;
                    double need3m1 = 0.0D;
                    double need2w = 0.0D;
                    double need2w1 = 0.0D;
                    String bc = "null";
                    String part = "null";
                    String supplier = "null";
                    int n = 0;
                    String beginyr = "null";
                    String endyr = "null";
                    int byr = 0;
                    int eyr = 0;
                    do {
                        if (!rs2.next())
                            break;
                        tyg_tw_p = rs2.getDouble("tyg_tw_p");
                        tyg_tx_p = rs2.getDouble("tyg_tx_p");
                        gd_p = rs2.getDouble("gd_p");
                        gn_p = rs2.getDouble("gn_p");
                        kv_p = rs2.getDouble("kv_p");
                        depo_p = rs2.getDouble("depo_p");
                        ks_p = rs2.getDouble("ks_p");
                        tyg_tw_bo = rs2.getDouble("tyg_tw_bo");
                        gd_bo = rs2.getDouble("gd_bo");
                        instock = rs2.getDouble("instock");
                        shipping = rs2.getDouble("shipping");
                        onorder = rs2.getDouble("onorder");
                        t12m = rs2.getInt("t12m");
                        t3msl = rs2.getInt("3msl");
                        supplier = rs2.getString("supplier");
                        n = rs2.getInt("new");
                        beginyr = rs2.getString("beginyr");
                        endyr = rs2.getString("endyr");
                        if (beginyr == null)
                            beginyr = "1900";
                        if (endyr == null)
                            endyr = "1900";
                        byr = Integer.parseInt(beginyr);
                        eyr = Integer.parseInt(endyr);
                        if (supplier == null)
                            supplier = " ";
                        if (instock < 0.0D)
                            instock = 0.0D;
                        bc = dnk.comparePrice1(gn_p, depo_p, tyg_tx_p, ks_p, tyg_tw_p, 0.0D, kv_p, supplier);
                        part = rs2.getString("part#");
                        need3m = dnk.getNeed(t12m, t3msl);
                        need3m1 = dnk.getNeed1(t12m, byr, eyr, n, t3msl);
                        need2w = dnk.getNeedLT(t12m);
                        need2w1 = dnk.getNeedLT1(t12m, byr, eyr, n, t3msl);
                        try {
                            st2.execute((new StringBuilder()).append("update tblmrn_tempout2 set [MOQ] = ").append(need3m).append(", [2w] = ").append(need2w).append(" where [part#] = '").append(part).append("';").toString());
                        } catch (SQLException er) {
                        }
                        if (instock + shipping + onorder + tyg_tw_bo + gd_bo <= need3m && need3m1 > 0.0D)
                            needBuy5 = need3m1 + (need3m - (instock + shipping + onorder + tyg_tw_bo + gd_bo));
                        else
                            needBuy5 = 0.0D;
                        if (instock + shipping + onorder + tyg_tw_bo + gd_bo <= need2w && need2w1 > 0.0D)
                            needBuy4 = need2w1 + (need2w - (instock + shipping + onorder + tyg_tw_bo + gd_bo));
                        else
                            needBuy4 = 0.0D;
                        if (bc.equals("tyg_tx") || bc.equals("gn") || bc.equals("kv") || bc.equals("depo") || bc.equals("ks")) {
                            if (bc.equals("tyg_tx"))
                                try {
                                    st2.execute((new StringBuilder()).append("update tblmrn_TEMPOUT2 set supplier = 'tx' where [part#] = '").append(part).append("';").toString());
                                    st2.execute((new StringBuilder()).append("update tblmrn set supplier = 'tx' where [part#] = '").append(part).append("';").toString());
                                } catch (SQLException er) {
                                }
                            else if (bc.equals("gn"))
                                try {
                                    st2.execute((new StringBuilder()).append("update tblmrn_TEMPOUT2 set supplier = 'gn' where [part#] = '").append(part).append("';").toString());
                                    st2.execute((new StringBuilder()).append("update tblmrn set supplier = 'gn' where [part#] = '").append(part).append("';").toString());
                                } catch (SQLException er) {
                                }
                            else if (bc.equals("kv"))
                                try {
                                    st2.execute((new StringBuilder()).append("update tblmrn_TEMPOUT2 set supplier = 'kv' where [part#] = '").append(part).append("';").toString());
                                    st2.execute((new StringBuilder()).append("update tblmrn set supplier = 'kv' where [part#] = '").append(part).append("';").toString());
                                } catch (SQLException er) {
                                }
                            else if (bc.equals("depo"))
                                try {
                                    st2.execute((new StringBuilder()).append("update tblmrn_TEMPOUT2 set supplier = 'd' where [part#] = '").append(part).append("';").toString());
                                    st2.execute((new StringBuilder()).append("update tblmrn set supplier = 'd' where [part#] = '").append(part).append("';").toString());
                                } catch (SQLException er) {
                                }
                            else if (bc.equals("ks"))
                                try {
                                    st2.execute((new StringBuilder()).append("update tblmrn_TEMPOUT2 set supplier = 'ks' where [part#] = '").append(part).append("';").toString());
                                    st2.execute((new StringBuilder()).append("update tblmrn set supplier = 'ks' where [part#] = '").append(part).append("';").toString());
                                } catch (SQLException er) {
                                }
                            if (needBuy4 > 0.0D)
                                if (bc.equals("tyg_tx"))
                                    try {
                                        st2.execute((new StringBuilder()).append("update tblmrn_tempout2 set tyg_tx = ").append(needBuy4).append(" where [part#] = '").append(part).append("';").toString());
                                    } catch (SQLException er) {
                                    }
                                else if (bc.equals("gn"))
                                    try {
                                        st2.execute((new StringBuilder()).append("update tblmrn_tempout2 set gn = ").append(needBuy4).append(" where [part#] = '").append(part).append("';").toString());
                                    } catch (SQLException er) {
                                    }
                                else if (bc.equals("kv"))
                                    try {
                                        st2.execute((new StringBuilder()).append("update tblmrn_tempout2 set kv = ").append(needBuy4).append(" where [part#] = '").append(part).append("';").toString());
                                    } catch (SQLException er) {
                                    }
                                else if (bc.equals("depo"))
                                    try {
                                        st2.execute((new StringBuilder()).append("update tblmrn_tempout2 set depo = ").append(needBuy4).append(" where [part#] = '").append(part).append("';").toString());
                                    } catch (SQLException er) {
                                    }
                                else if (bc.equals("ks"))
                                    try {
                                        st2.execute((new StringBuilder()).append("update tblmrn_tempout2 set ks = ").append(needBuy4).append(" where [part#] = '").append(part).append("';").toString());
                                    } catch (SQLException er) {
                                    }
                        } else {
                            if (bc.equals("tyg_tw"))
                                try {
                                    st2.execute((new StringBuilder()).append("update tblmrn_TEMPOUT2 set supplier = 'ty' where [part#] = '").append(part).append("';").toString());
                                    st2.execute((new StringBuilder()).append("update tblmrn set supplier = 'ty' where [part#] = '").append(part).append("';").toString());
                                } catch (SQLException er) {
                                }
                            else if (bc.equals("gd"))
                                try {
                                    st2.execute((new StringBuilder()).append("update tblmrn_TEMPOUT2 set supplier = 'gd' where [part#] = '").append(part).append("';").toString());
                                    st2.execute((new StringBuilder()).append("update tblmrn set supplier = 'gd' where [part#] = '").append(part).append("';").toString());
                                } catch (SQLException er) {
                                }
                            if (needBuy5 > 0.0D)
                                if (bc.equals("tyg_tw"))
                                    try {
                                        st2.execute((new StringBuilder()).append("update tblmrn_tempout2 set tyg_tw = ").append(needBuy5).append(" where [part#] = '").append(part).append("';").toString());
                                    } catch (SQLException er) {
                                    }
                                else if (bc.equals("gd"))
                                    try {
                                        st2.execute((new StringBuilder()).append("update tblmrn_tempout2 set gd = ").append(needBuy5).append(" where [part#] = '").append(part).append("';").toString());
                                    } catch (SQLException er) {
                                    }
                                else
                                    try {
                                        st2.execute((new StringBuilder()).append("update tblmrn_tempout2 set manual=-1 where [part#] = '").append(part).append("';").toString());
                                    } catch (SQLException er) {
                                    }
                        }
                    } while (true);
                }
                st.execute("select * from tblmrn_tempout2;");
                ResultSet rs1 = st.getResultSet();
                if (rs1 != null) {
                    int tyg_tw = 0;
                    int gd = 0;
                    int tyg_tw_k = 0;
                    int gd_k = 0;
                    int tyg_tw_buy = 0;
                    int t12m = 0;
                    double tyg_tw_a = 0.0D;
                    double gd_a = 0.0D;
                    double tyg_tw_b = 0.0D;
                    double gd_b = 0.0D;
                    double instock = 0.0D;
                    int tyg_tw_bo = 0;
                    int gd_bo = 0;
                    int onorder = 0;
                    int shipping = 0;
                    String part = "null";
                    do {
                        if (!rs1.next())
                            break;
                        tyg_tw = rs1.getInt("tyg_tw");
                        gd = rs1.getInt("gd");
                        tyg_tw_k = rs1.getInt("tyg_tw_k");
                        gd_k = rs1.getInt("gd_k");
                        t12m = rs1.getInt("t12m");
                        part = rs1.getString("part#");
                        instock = rs1.getDouble("instock");
                        onorder = rs1.getInt("onorder");
                        shipping = rs1.getInt("shipping");
                        tyg_tw_bo = rs1.getInt("tyg_tw_bo");
                        gd_bo = rs1.getInt("gd_bo");
                        if (instock < 0.0D)
                            instock = 0.0D;
                        if (t12m > 3) {
                            if (tyg_tw > 0 && tyg_tw_k >= 1 && tyg_tw_k <= 7)
                                try {
                                    tyg_tw_a = (double) tyg_tw / (double) tyg_tw_k;
                                    tyg_tw_b = Math.ceil(tyg_tw_a) * (double) tyg_tw_k + instock + (double) onorder + (double) shipping + (double) tyg_tw_bo + (double) gd_bo;
                                    if (tyg_tw_b <= (double) (t12m * 2))
                                        st1.execute((new StringBuilder()).append("UPDATE TBLMRN_TEMPOUT2 SET TYG_TW = ").append(Math.ceil(tyg_tw_a) * (double) tyg_tw_k).append(" where [part#] = '").append(part).append("';").toString());
                                } catch (SQLException er) {
                                }
                            if (gd > 0 && gd_k >= 1 && gd_k <= 7)
                                try {
                                    gd_a = (double) gd / (double) gd_k;
                                    gd_b = Math.ceil(gd_a) * (double) gd_k + instock + (double) onorder + (double) shipping + (double) tyg_tw_bo + (double) gd_bo;
                                    if (gd_b <= (double) (t12m * 2))
                                        st1.execute((new StringBuilder()).append("UPDATE TBLMRN_TEMPOUT2 SET GD = ").append(Math.ceil(gd_a) * (double) gd_k).append(" where [part#] = '").append(part).append("';").toString());
                                } catch (SQLException er) {
                                }
                        }
                    } while (true);
                }
                st.execute("UPDATE TBLMRN_TEMPOUT2 SET GN = iif(Round((TYG_TW + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)>(instock + onorder + shipping + tyg_tw_bo + gd_bo),Round((TYG_TW + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)-(instock + onorder + shipping + tyg_tw_bo + gd_bo),0), TYG_TW=0 WHERE TYG_TW > 0 AND TYG_TW < TYG_TW_K AND GN_P <= IIF(TYG_TX_P = 0,9999,TYG_TX_P) AND GN_P <= IIF(KV_P = 0,9999,KV_P) AND GN_P <= IIF(DEPO_P = 0,9999,DEPO_P) AND GN_P <= IIF(KS_P = 0,9999,KS_P) AND GN_P <> 0;");
                st.execute("UPDATE TBLMRN_TEMPOUT2 SET TYG_TX = iif(Round((TYG_TW + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)>(instock + onorder + shipping + tyg_tw_bo + gd_bo),Round((TYG_TW + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)-(instock + onorder + shipping + tyg_tw_bo + gd_bo),0), TYG_TW=0 WHERE TYG_TW > 0 AND TYG_TW < TYG_TW_K AND TYG_TX_P < IIF(GN_P = 0,9999,GN_P) AND TYG_TX_P <= IIF(KV_P = 0,9999,KV_P) AND TYG_TX_P < IIF(DEPO_P = 0,9999,DEPO_P) AND TYG_TX_P <= IIF(KS_P = 0,9999,KS_P) AND TYG_TX_P <> 0;");
                st.execute("UPDATE TBLMRN_TEMPOUT2 SET KS = iif(Round((TYG_TW + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)>(instock + onorder + shipping + tyg_tw_bo + gd_bo),Round((TYG_TW + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)-(instock + onorder + shipping + tyg_tw_bo + gd_bo),0), TYG_TW=0 WHERE TYG_TW > 0 AND TYG_TW < TYG_TW_K AND KS_P < IIF(GN_P = 0,9999,GN_P) AND KS_P <= IIF(KV_P = 0,9999,KV_P) AND KS_P < IIF(DEPO_P = 0,9999,DEPO_P) AND KS_P < IIF(TYG_TX_P = 0,9999,TYG_TX_P) AND KS_P <> 0;");
                st.execute("UPDATE TBLMRN_TEMPOUT2 SET KV = iif(Round((TYG_TW + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)>(instock + onorder + shipping + tyg_tw_bo + gd_bo),Round((TYG_TW + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)-(instock + onorder + shipping + tyg_tw_bo + gd_bo),0), TYG_TW=0 WHERE TYG_TW > 0 AND TYG_TW < TYG_TW_K AND KV_P < IIF(GN_P = 0,9999,GN_P) AND KV_P < IIF(KS_P = 0,9999,KS_P) AND KV_P < IIF(DEPO_P = 0,9999,DEPO_P) AND KV_P < IIF(TYG_TX_P = 0,9999,TYG_TX_P) AND KV_P <> 0;");
                st.execute("UPDATE TBLMRN_TEMPOUT2 SET DEPO = iif(Round((TYG_TW + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)>(instock + onorder + shipping + tyg_tw_bo + gd_bo),Round((TYG_TW + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)-(instock + onorder + shipping + tyg_tw_bo + gd_bo),0), TYG_TW=0 WHERE TYG_TW > 0 AND TYG_TW < TYG_TW_K AND DEPO_P < IIF(GN_P = 0,9999,GN_P) AND DEPO_P <= IIF(KS_P = 0,9999,KS_P) AND DEPO_P <= IIF(KV_P = 0,9999,KV_P) AND DEPO_P <= IIF(TYG_TX_P = 0,9999,TYG_TX_P) AND DEPO_P <> 0;");
                st.execute("UPDATE TBLMRN_TEMPOUT2 SET MANUAL = -1 WHERE TYG_TW > 0 AND TYG_TW < IIF(TYG_TW_K>0,TYG_TW_K,999);");
                st.execute("UPDATE TBLMRN_TEMPOUT2 SET GN = iif(Round((GD + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)>(instock + onorder + shipping + tyg_tw_bo + gd_bo),Round((GD + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)-(instock + onorder + shipping + tyg_tw_bo + gd_bo),0), GD=0 WHERE GD > 0 AND GD < GD_K AND GN_P <= IIF(TYG_TX_P = 0,9999,TYG_TX_P) AND GN_P <= IIF(KV_P = 0,9999,KV_P) AND GN_P <= IIF(DEPO_P = 0,9999,DEPO_P) AND GN_P <= IIF(KS_P = 0,9999,KS_P) AND GN_P <> 0;");
                st.execute("UPDATE TBLMRN_TEMPOUT2 SET TYG_TX = iif(Round((GD + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)>(instock + onorder + shipping + tyg_tw_bo + gd_bo),Round((GD + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)-(instock + onorder + shipping + tyg_tw_bo + gd_bo),0), GD=0 WHERE GD > 0 AND GD < GD_K AND TYG_TX_P < IIF(GN_P = 0,9999,GN_P) AND TYG_TX_P <= IIF(KS_P = 0,9999,KS_P) AND TYG_TX_P <= IIF(KV_P = 0,9999,KV_P) AND TYG_TX_P < IIF(DEPO_P = 0,9999,DEPO_P) AND TYG_TX_P <> 0;");
                st.execute("UPDATE TBLMRN_TEMPOUT2 SET KS = iif(Round((GD + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)>(instock + onorder + shipping + tyg_tw_bo + gd_bo),Round((GD + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)-(instock + onorder + shipping + tyg_tw_bo + gd_bo),0), GD=0 WHERE GD > 0 AND GD < GD_K AND KS_P < IIF(GN_P = 0,9999,GN_P) AND KS_P <= IIF(KV_P = 0,9999,KV_P) AND KS_P < IIF(DEPO_P = 0,9999,DEPO_P) AND KS_P < IIF(TYG_TX_P = 0,9999,TYG_TX_P) AND KS_P <> 0;");
                st.execute("UPDATE TBLMRN_TEMPOUT2 SET KV = iif(Round((GD + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)>(instock + onorder + shipping + tyg_tw_bo + gd_bo),Round((GD + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)-(instock + onorder + shipping + tyg_tw_bo + gd_bo),0), GD=0 WHERE GD > 0 AND GD < GD_K AND KV_P < IIF(GN_P = 0,9999,GN_P) AND KV_P < IIF(KS_P = 0,9999,KS_P) AND KV_P < IIF(DEPO_P = 0,9999,DEPO_P) AND KV_P < IIF(TYG_TX_P = 0,9999,TYG_TX_P) AND KV_P <> 0;");
                st.execute("UPDATE TBLMRN_TEMPOUT2 SET DEPO = iif(Round((GD + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)>(instock + onorder + shipping + tyg_tw_bo + gd_bo),Round((GD + instock + onorder + shipping + tyg_tw_bo + gd_bo)/2,0)-(instock + onorder + shipping + tyg_tw_bo + gd_bo),0), GD=0 WHERE GD > 0 AND GD < GD_K AND DEPO_P < IIF(GN_P = 0,9999,GN_P) AND DEPO_P <= IIF(KS_P = 0,9999,KS_P) AND DEPO_P <= IIF(KV_P = 0,9999,KV_P) AND DEPO_P <= IIF(TYG_TX_P = 0,9999,TYG_TX_P) AND DEPO_P <> 0;");
                st.execute("UPDATE TBLMRN_TEMPOUT2 SET MANUAL = -1 WHERE GD > 0 AND GD < IIF(GD_K>0,GD_K,999);");
            } catch (SQLException er) {
                er.printStackTrace();
            }
            st.close();
            st1.close();
            st2.close();
            st3.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error message: "+e);
        }
    }
}
